package com.example.smartlearning.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.smartlearning.dao.NewDao;
import com.example.smartlearning.entity.NewEntity;
import com.example.smartlearning.entity.ResultEntity;
import com.example.smartlearning.service.NewService;
import com.example.smartlearning.tool.SortTool;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class NewServiceImpl extends ServiceImpl<NewDao, NewEntity> implements NewService {

    @Resource
    NewDao newDao;

    @Resource
    SortTool sortTool;

    private static final Logger logger = LoggerFactory.getLogger(NewServiceImpl.class);//日志。getLogger需要传入当前的类名

    /**
     * @param start 开始
     * @param limit 页面限制
     * @return 返回
     */
    public String selectAllNews(int start, int limit) {
        try {
            return ResultEntity.success(newDao.selectAllNew(start, limit));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }


    /**
     * 根据用户展示首页的新闻
     *
     * @param interest 兴趣
     * @return 返回
     */
    @Override
    public String selectByUser(String interest) {
        String interests = sortTool.sortToInteger(interest);
        try {
            return ResultEntity.success(newDao.selectByUser(interests));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 根据传入的新闻id获取新闻详情
     *
     * @param newId 新闻id
     * @return 返回
     */
    @Override
    public String selectNewDetail(int newId) {
        try {
            return ResultEntity.success(newDao.selectNewDetail(newId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 根据id更新新闻
     *
     * @param newId      id
     * @param newTitle   标题
     * @param newDetail  详情
     * @param createTime 创建时间
     * @param newSort    分类
     * @param newInImg   详情图片
     * @param newImg     封面图片
     * @return 返回
     */
    @Override
    public String updateNewById(int newId, String newTitle, String newDetail, String createTime, String newSort, String newInImg, String newImg) {
        try {
            return ResultEntity.success(newDao.updateNewById(newId, newTitle, newDetail, createTime, newSort, newInImg, newImg));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * @param newTitle   标题
     * @param newDetail  详情
     * @param createTime 创建时间
     * @param newSort    分类
     * @param newUser    用户
     * @param newInImg   详情图片
     * @param newImg     封面图片
     * @return 返回
     */
    @Override
    public String addNew(String newTitle, String newDetail, String createTime, String newSort, int newUser, String newInImg, String newImg) {
        try {
            return ResultEntity.success(newDao.addNew(newTitle, newDetail, createTime, newSort, newUser, newInImg, newImg));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 删除新闻
     *
     * @param id id
     * @return 返回
     */
    @Override
    public String deleteByNewId(int id) {
        try {
            return ResultEntity.success(newDao.deleteByNewId(id));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }


}
